package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.AccountDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.Account;
import com.tbit.main.service.AccountService;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 角色
 *
 * @author Leon
 * 2019年11月9日 下午2:09:42
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Override
    public List<Account> getByAccountIds(List<Integer> accountIds) {
        Map<String, Object> params = new HashMap<>();
        params.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        return accountDao.getByAccountIds(params);
    }

    @Override
    public List<Account> getAccountIds(List<Integer> accountIds) {
        return CollUtil.isNotEmpty(accountIds)
                ? accountDao.getAccountIds(StringUtil.getListString(accountIds))
                : Collections.emptyList();
    }

    @Override
    public List<Account> getByParentId(Integer parentId) {
        return accountDao.getByParentId(parentId);
    }

    @Override
    public List<Account> getBrandAgent(Integer accountId) {
        return accountDao.getBrandAgent(accountId);
    }

    @Override
    public Account getByAccountId(Integer accountId) {
        return accountDao.getByAccountId(accountId);
    }

    @Override
    public List<Account> getHolderAgent(Integer accountUserId) {
        return accountDao.getHolderAgent(accountUserId);
    }

    @Override
    public long getCountHolderAgent(Integer accountUserId) {
        return accountDao.selectCountHolder(accountUserId);
    }

	/*@Override
	public Account getBrandConfig(Integer accountId, Integer time){
		time++;
		if(time.equals(10)){
			throw new BaseException("Recursion.Error");
		}
		Account account = accountDao.getByAccountId(accountId);
		if(account.getType().equals(1)){
			return account;
		}
		return getBrandConfig(account.getParentId(),time);
	}*/

    @Override
    public List<Account> getAll() {
        return accountDao.getAll();
    }

    @Override
    public Account getByAccountAndType(Integer accountId, Integer type) {
        Map<String, Object> params = new HashMap<>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("type", type);
        return accountDao.getByAccountAndType(params);
    }

    @Override
    public List<Account> getByType(Integer type) {
        return accountDao.getByType(type);
    }

    @Override
    public List<Account> getAccountIdsVague(List<Integer> accountIds, String accountName) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        map.put("accountName", "%" + accountName + "%");
        return accountDao.getAccountIdsVague(map);
    }

    @Override
    public Integer getByAccount(Integer accountId) {
        Integer brandId = accountDao.getByAccount(accountId);
        if (brandId == null){
            throw new BaseException("account.not.exist");
        }
        return accountDao.getByAccount(accountId);
    }

    @Override
    public List<Map<String, Object>> getAccountAll() {
        return accountDao.getAccountAll();
    }

    /**
     * 获取品牌id-区域id
     *
     * @return
     */
    @Override
    public Map<Integer, List<Integer>> getStageBrandIdAccountIdS() {
        /**品牌id-区域id*/
        Map<Integer, List<Integer>> brandIdAccountMap = new HashMap<>();
        /**区域id-品牌id*/
        List<Map<String, Object>> accountIdPrendId = getAccountAll();
        for (int i = 0; i < accountIdPrendId.size(); i++) {
            try {
                if (brandIdAccountMap.containsKey(Integer.parseInt(accountIdPrendId.get(i).get("parentId").toString()))) {
                    brandIdAccountMap.get(Integer.parseInt(accountIdPrendId.get(i).get("parentId").toString())).add(Integer.parseInt(accountIdPrendId.get(i).get("accountId").toString()));
                } else {
                    List<Integer> accountIds = new LinkedList<>();
                    accountIds.add(Integer.parseInt(accountIdPrendId.get(i).get("accountId").toString()));
                    brandIdAccountMap.put(Integer.parseInt(accountIdPrendId.get(i).get("parentId").toString()), accountIds);
                }
            } catch (Exception e) {
                LogUtil.error(e.toString(), e);
            }
        }
        return brandIdAccountMap;
    }
}
